package cn.iocoder.yudao.module.brokerage.record.convert;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.number.MoneyUtils;
import cn.iocoder.yudao.module.api.brokerage.record.dto.BrokerageRecordSaveDTO;
import cn.iocoder.yudao.module.api.brokerage.record.enums.BrokerageBonusLevelEnum;
import cn.iocoder.yudao.module.api.brokerage.record.enums.BrokerageRecordBizTypeEnum;
import cn.iocoder.yudao.module.api.brokerage.record.enums.BrokerageRecordStatusEnum;
import cn.iocoder.yudao.module.brokerage.record.pojo.po.BrokerageRecordDO;
import cn.iocoder.yudao.module.brokerage.record.pojo.vo.AdminBrokerageRecordVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  这里是文件说明
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/01/22
 */
@Mapper
public interface BrokerageRecordConvert {

    BrokerageRecordConvert INSTANCE = Mappers.getMapper(BrokerageRecordConvert.class);

    default List<BrokerageRecordDO> convertList(Long userId, List<BrokerageRecordSaveDTO> list) {
        List<BrokerageRecordDO> records = new ArrayList<>(list.size());
        for (BrokerageRecordSaveDTO item : list) {
            BrokerageRecordDO record = new BrokerageRecordDO();
            record.setUserId(userId);
            record.setBizType(item.getBizType());
            record.setBizId(item.getBizId());
            record.setBasePrice(item.getBasePrice());
            record.setBonusLevel(item.getBonusLevel());
            double bonusPercent = item.getBonusPercent();
            int bonusMoney = MoneyUtils.calculateRatePriceFloor(item.getBasePrice(), bonusPercent);
            record.setBonusPercent(bonusPercent);
            record.setBonusMoney(bonusMoney);
            record.setSourceUserId(item.getSourceUserId());
            if (bonusMoney == 0) {
                record.setStatus(BrokerageRecordStatusEnum.CLOSED.getValue());
                record.setUnfreezeTime(LocalDateTime.now());
                record.setCancelTime(LocalDateTime.now());
            } else {
                record.setStatus(BrokerageRecordStatusEnum.FREEZING.getValue());
                record.setUnfreezeTime(LocalDateTime.now().plusDays(15));
                record.setCancelTime(null);
            }
            if (StrUtil.isNotBlank(item.getNote())) {
                record.setNote(item.getNote());
            } else {
                record.setNote(BrokerageRecordBizTypeEnum.codeOf(item.getBizType()).getSummary()
                        .formatted(BrokerageBonusLevelEnum.valueOf(item.getBonusLevel()).getName(), MoneyUtils.fenToYuanStr(bonusMoney)));
            }
             records.add(record);
        }
        return records;
    }

    @Mapping(target = "user", ignore = true)
    @Mapping(target = "sourceUser", ignore = true)
    @Mapping(target = "bonusLevelName", ignore = true)
    @Mapping(target = "bizTypeName", ignore = true)
    AdminBrokerageRecordVO convert(BrokerageRecordDO bean);

    List<AdminBrokerageRecordVO> convertList2(List<BrokerageRecordDO> list);

}
